package rpd.model;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

public class GeneratorSqlMapCreater {

	private void generator(){
		System.out.println("generatorConfig.xml……逆向工程开始");
		List<String> warnigngs = new ArrayList<String>();
		boolean overwrite = true;
		
		File configFile = new File("src/main/resources/generatorConfig.xml");
		ConfigurationParser cp = new ConfigurationParser(warnigngs);
		Configuration config = null;
		try {
			config = cp.parseConfiguration(configFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("文件读取失败……");
			e.printStackTrace();
		} catch (XMLParserException e) {
			// TODO Auto-generated catch block
			System.out.println("xml解析失败……");
			e.printStackTrace();
		}
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator = null;
		try {
			myBatisGenerator = new MyBatisGenerator(config, callback, warnigngs);
		} catch (InvalidConfigurationException e) {
			// TODO Auto-generated catch block
			System.out.println("generatorConfig.xml配置文件无效……");
			e.printStackTrace();
		}
		try {
			myBatisGenerator.generate(null);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("generatorConfig.xml配置文件中，SQL异常……");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("generatorConfig.xml……逆向工程结束");
	}
	
	public static void main(String[] args) {
		GeneratorSqlMapCreater creater = new GeneratorSqlMapCreater();
		creater.generator();
	}
}
